export class Enemy {
    constructor(config) {
        this.id = config.id || Math.random().toString(36).substr(2, 9);
        this.name = config.name || 'Unknown Enemy';
        this.maxHealth = config.maxHealth || 40;
        this.health = config.health !== undefined ? config.health : this.maxHealth;
        this.block = config.block || 0;
        this.intents = config.intents || [];
        this.currentIntent = null;
        this.statusEffects = {};
        this.moveSet = config.moveSet || [];
    }

    startTurn() {
        // 选择行动意图
        this.chooseIntent();
        
        // 处理回合开始时的状态效果
        this.processStartOfTurnEffects();
    }

    chooseIntent() {
        // 随机选择一个行动
        if (this.moveSet.length > 0) {
            const moveIndex = Math.floor(Math.random() * this.moveSet.length);
            this.currentIntent = this.moveSet[moveIndex];
        } else {
            // 默认行动：攻击
            this.currentIntent = {
                type: 'attack',
                value: 6
            };
        }
    }

    executeIntent(target) {
        if (!this.currentIntent) return;
        
        switch (this.currentIntent.type) {
            case 'attack':
                // 攻击目标
                const damage = this.calculateDamage(this.currentIntent.value);
                target.takeDamage(damage);
                break;
                
            case 'defend':
                // 增加格挡
                this.addBlock(this.currentIntent.value);
                break;
                
            case 'buff':
                // 增加自身状态
                this.addStatus(this.currentIntent.status, this.currentIntent.value);
                break;
                
            case 'debuff':
                // 给目标添加负面状态
                target.addStatus(this.currentIntent.status, this.currentIntent.value);
                break;
                
            case 'special':
                // 特殊行动，由具体敌人子类实现
                this.specialAction(target);
                break;
        }
    }

    calculateDamage(baseDamage) {
        let damage = baseDamage;
        
        // 应用力量加成
        if (this.statusEffects.strength) {
            damage += this.statusEffects.strength;
        }
        
        // 应用虚弱减益
        if (this.statusEffects.weak) {
            damage = Math.floor(damage * 0.75);
        }
        
        return damage > 0 ? damage : 0;
    }

    takeDamage(amount) {
        // 先减少格挡
        if (this.block > 0) {
            if (amount <= this.block) {
                this.block -= amount;
                amount = 0;
            } else {
                amount -= this.block;
                this.block = 0;
            }
        }
        
        // 再减少生命值
        if (amount > 0) {
            // 如果有易伤状态，增加伤害
            if (this.statusEffects.vulnerable) {
                amount = Math.floor(amount * 1.5);
            }
            
            this.health -= amount;
            if (this.health < 0) this.health = 0;
        }
        
        return amount;
    }

    addBlock(amount) {
        this.block += amount;
        return this.block;
    }

    addStatus(statusType, amount) {
        if (!this.statusEffects[statusType]) {
            this.statusEffects[statusType] = 0;
        }
        this.statusEffects[statusType] += amount;
    }

    processStartOfTurnEffects() {
        // 处理回合开始时的状态效果
    }

    processEndOfTurnEffects() {
        // 处理回合结束时的状态效果
        if (this.statusEffects.poison > 0) {
            this.takeDamage(this.statusEffects.poison);
            this.statusEffects.poison--;
        }
        
        // 减少状态效果持续时间
        for (const status in this.statusEffects) {
            if (this.statusEffects[status] > 0) {
                this.statusEffects[status]--;
            }
        }
    }

    getIntentDisplay() {
        // 返回意图显示信息
        if (!this.currentIntent) return { text: '???', type: 'unknown' };
        
        switch (this.currentIntent.type) {
            case 'attack':
                return { 
                    text: `攻击 ${this.calculateDamage(this.currentIntent.value)}`, 
                    type: 'attack' 
                };
                
            case 'defend':
                return { 
                    text: `防御 ${this.currentIntent.value}`, 
                    type: 'defend' 
                };
                
            case 'buff':
                return { 
                    text: `增益 ${this.currentIntent.status}`, 
                    type: 'buff' 
                };
                
            case 'debuff':
                return { 
                    text: `减益 ${this.currentIntent.status}`, 
                    type: 'debuff' 
                };
                
            case 'special':
                return { 
                    text: '特殊行动', 
                    type: 'special' 
                };
                
            default:
                return { 
                    text: '???', 
                    type: 'unknown' 
                };
        }
    }

    specialAction(target) {
        // 由子类实现的特殊行动
        console.log(`${this.name} 执行了特殊行动`);
    }

    isDead() {
        return this.health <= 0;
    }
}